<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2015-5-23
 * Time: 13:36
 */

namespace doumiaoduo\lhgdialog;


use yii\base\Widget;

class LhgDialog extends Widget{

    public $text='';

    public $cssClass;

    public $dialogOptions=[];

    private $_rel_no;

    public function init(){
        $this->_rel_no='lhgdialog_'.rand(10000000,99999999);

    }

    public function run()
    {
        echo '<a class="'.$this->cssClass.'" style="cursor:pointer" rel="'.$this->_rel_no.'">'.$this->text.'</a>';
        $this->registerClientScript();
    }

    public function registerClientScript()
    {
        $view = $this->getView();
        LhgDialogAsset::register($view);
        $js_str="$(document).on('click','a[rel={$this->_rel_no}]',function(){ \n $.dialog({";
        $js_str.='min:false,max:false,lock:true,';
        foreach($this->dialogOptions as $k=>$v){
            if($k=='title'||$k=='content'||$k=='cancelVal'||$k=='left'||$k=='okVal'||$k=='top'||$k=='background'||$k=='opacity'||$k=='padding'||$k=='skin'||$k=='id'||$k=='width'||$k=='height'){
                $js_str.="{$k}:'{$v}',\n";
            }
            else{
                $js_str.="{$k}:{$v},\n";
            }
        }
        $js_str.="});})";
        $view->registerJs($js_str);
    }


}